package o;

import java.io.Closeable;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

final class fwg implements Closeable {
    static final Logger ˏ = Logger.getLogger(fwi.class.getName());
    private final b ˊ = new b(this.ˎ);
    private final boolean ˋ;
    private final fxg ˎ;
    final e ॱ = new e(4096, this.ˊ);

    static final class b implements fxw {
        int ˊ;
        int ˋ;
        int ˎ;
        short ˏ;
        byte ॱ;
        private final fxg ॱॱ;

        b(fxg o_fxg) {
            this.ॱॱ = o_fxg;
        }

        public long ˊ(fxi o_fxi, long j) throws IOException {
            while (this.ˊ == 0) {
                this.ॱॱ.ʼ((long) this.ˏ);
                this.ˏ = (short) 0;
                if ((this.ॱ & 4) != 0) {
                    return -1;
                }
                ˏ();
            }
            long ˊ = this.ॱॱ.ˊ(o_fxi, Math.min(j, (long) this.ˊ));
            if (ˊ == -1) {
                return -1;
            }
            this.ˊ = (int) (((long) this.ˊ) - ˊ);
            return ˊ;
        }

        public fxv ˊ() {
            return this.ॱॱ.ˊ();
        }

        public void close() throws IOException {
        }

        private void ˏ() throws IOException {
            int i = this.ˎ;
            int ˏ = fwg.ˏ(this.ॱॱ);
            this.ˊ = ˏ;
            this.ˋ = ˏ;
            byte ʻ = (byte) (this.ॱॱ.ʻ() & 255);
            this.ॱ = (byte) (this.ॱॱ.ʻ() & 255);
            if (fwg.ˏ.isLoggable(Level.FINE)) {
                fwg.ˏ.fine(fwi.ॱ(true, this.ˎ, this.ˋ, ʻ, this.ॱ));
            }
            this.ˎ = this.ॱॱ.ˋॱ() & ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
            if (ʻ != (byte) 9) {
                throw fwi.ˋ("%s != TYPE_CONTINUATION", Byte.valueOf(ʻ));
            } else if (this.ˎ != i) {
                throw fwi.ˋ("TYPE_CONTINUATION streamId changed", new Object[0]);
            }
        }
    }

    interface c {
        void ˊ(int i, fwf o_fwf, fxh o_fxh);

        void ˊ(boolean z, int i, int i2);

        void ˊ(boolean z, int i, fxg o_fxg, int i2) throws IOException;

        void ˏ();

        void ˏ(boolean z, int i, int i2, List<fwb> list);

        void ॱ(int i, int i2, int i3, boolean z);

        void ॱ(int i, int i2, List<fwb> list) throws IOException;

        void ॱ(int i, long j);

        void ॱ(int i, fwf o_fwf);

        void ॱ(boolean z, fwl o_fwl);
    }

    fwg(fxg o_fxg, boolean z) {
        this.ˎ = o_fxg;
        this.ˋ = z;
    }

    public void ˊ(c cVar) throws IOException {
        if (!this.ˋ) {
            fxh ˋ = this.ˎ.ˋ((long) fwi.ˊ.ʼ());
            if (ˏ.isLoggable(Level.FINE)) {
                ˏ.fine(fvg.ˊ("<< CONNECTION %s", ˋ.ᐝ()));
            }
            if (!fwi.ˊ.equals(ˋ)) {
                throw fwi.ˋ("Expected a connection header but was %s", ˋ.ˊ());
            }
        } else if (!ˊ(true, cVar)) {
            throw fwi.ˋ("Required SETTINGS preface not received", new Object[0]);
        }
    }

    public boolean ˊ(boolean z, c cVar) throws IOException {
        try {
            this.ˎ.ˎ(9);
            int ˏ = ˏ(this.ˎ);
            if (ˏ < 0 || ˏ > 16384) {
                throw fwi.ˋ("FRAME_SIZE_ERROR: %s", Integer.valueOf(ˏ));
            }
            byte ʻ = (byte) (this.ˎ.ʻ() & 255);
            if (!z || ʻ == (byte) 4) {
                byte ʻ2 = (byte) (this.ˎ.ʻ() & 255);
                int ˋॱ = this.ˎ.ˋॱ() & ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
                if (ˏ.isLoggable(Level.FINE)) {
                    ˏ.fine(fwi.ॱ(true, ˋॱ, ˏ, ʻ, ʻ2));
                }
                switch (ʻ) {
                    case (byte) 0:
                        ॱ(cVar, ˏ, ʻ2, ˋॱ);
                        return true;
                    case (byte) 1:
                        ˋ(cVar, ˏ, ʻ2, ˋॱ);
                        return true;
                    case (byte) 2:
                        ˏ(cVar, ˏ, ʻ2, ˋॱ);
                        return true;
                    case (byte) 3:
                        ˎ(cVar, ˏ, ʻ2, ˋॱ);
                        return true;
                    case (byte) 4:
                        ˊ(cVar, ˏ, ʻ2, ˋॱ);
                        return true;
                    case (byte) 5:
                        ᐝ(cVar, ˏ, ʻ2, ˋॱ);
                        return true;
                    case (byte) 6:
                        ʽ(cVar, ˏ, ʻ2, ˋॱ);
                        return true;
                    case (byte) 7:
                        ʻ(cVar, ˏ, ʻ2, ˋॱ);
                        return true;
                    case (byte) 8:
                        ॱॱ(cVar, ˏ, ʻ2, ˋॱ);
                        return true;
                    default:
                        this.ˎ.ʼ((long) ˏ);
                        return true;
                }
            }
            throw fwi.ˋ("Expected a SETTINGS frame but was %s", Byte.valueOf(ʻ));
        } catch (IOException e) {
            return false;
        }
    }

    private void ˋ(c cVar, int i, byte b, int i2) throws IOException {
        short s = (short) 0;
        if (i2 == 0) {
            throw fwi.ˋ("PROTOCOL_ERROR: TYPE_HEADERS streamId == 0", new Object[0]);
        }
        boolean z = (b & 1) != 0;
        if ((b & 8) != 0) {
            s = (short) (this.ˎ.ʻ() & 255);
        }
        if ((b & 32) != 0) {
            ॱ(cVar, i2);
            i -= 5;
        }
        cVar.ˏ(z, i2, -1, ॱ(ˋ(i, b, s), s, b, i2));
    }

    private List<fwb> ॱ(int i, short s, byte b, int i2) throws IOException {
        b bVar = this.ˊ;
        this.ˊ.ˊ = i;
        bVar.ˋ = i;
        this.ˊ.ˏ = s;
        this.ˊ.ॱ = b;
        this.ˊ.ˎ = i2;
        this.ॱ.ˎ();
        return this.ॱ.ॱ();
    }

    private void ॱ(c cVar, int i, byte b, int i2) throws IOException {
        short s = (short) 1;
        short s2 = (short) 0;
        if (i2 == 0) {
            throw fwi.ˋ("PROTOCOL_ERROR: TYPE_DATA streamId == 0", new Object[0]);
        }
        boolean z;
        if ((b & 1) != 0) {
            z = true;
        } else {
            z = false;
        }
        if ((b & 32) == 0) {
            s = (short) 0;
        }
        if (s != (short) 0) {
            throw fwi.ˋ("PROTOCOL_ERROR: FLAG_COMPRESSED without SETTINGS_COMPRESS_DATA", new Object[0]);
        }
        if ((b & 8) != 0) {
            s2 = (short) (this.ˎ.ʻ() & 255);
        }
        cVar.ˊ(z, i2, this.ˎ, ˋ(i, b, s2));
        this.ˎ.ʼ((long) s2);
    }

    private void ˏ(c cVar, int i, byte b, int i2) throws IOException {
        if (i != 5) {
            throw fwi.ˋ("TYPE_PRIORITY length: %d != 5", Integer.valueOf(i));
        } else if (i2 == 0) {
            throw fwi.ˋ("TYPE_PRIORITY streamId == 0", new Object[0]);
        } else {
            ॱ(cVar, i2);
        }
    }

    private void ॱ(c cVar, int i) throws IOException {
        int ˋॱ = this.ˎ.ˋॱ();
        cVar.ॱ(i, ˋॱ & ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED, (this.ˎ.ʻ() & 255) + 1, (Integer.MIN_VALUE & ˋॱ) != 0);
    }

    private void ˎ(c cVar, int i, byte b, int i2) throws IOException {
        if (i != 4) {
            throw fwi.ˋ("TYPE_RST_STREAM length: %d != 4", Integer.valueOf(i));
        } else if (i2 == 0) {
            throw fwi.ˋ("TYPE_RST_STREAM streamId == 0", new Object[0]);
        } else {
            fwf ˏ = fwf.ˏ(this.ˎ.ˋॱ());
            if (ˏ == null) {
                throw fwi.ˋ("TYPE_RST_STREAM unexpected error code: %d", Integer.valueOf(r0));
            } else {
                cVar.ॱ(i2, ˏ);
            }
        }
    }

    private void ˊ(c cVar, int i, byte b, int i2) throws IOException {
        if (i2 != 0) {
            throw fwi.ˋ("TYPE_SETTINGS streamId != 0", new Object[0]);
        } else if ((b & 1) != 0) {
            if (i != 0) {
                throw fwi.ˋ("FRAME_SIZE_ERROR ack frame should be empty!", new Object[0]);
            }
            cVar.ˏ();
        } else if (i % 6 != 0) {
            throw fwi.ˋ("TYPE_SETTINGS length %% 6 != 0: %s", Integer.valueOf(i));
        } else {
            fwl o_fwl = new fwl();
            for (int i3 = 0; i3 < i; i3 += 6) {
                int ॱˊ = this.ˎ.ॱˊ() & 65535;
                int ˋॱ = this.ˎ.ˋॱ();
                switch (ॱˊ) {
                    case 2:
                        if (!(ˋॱ == 0 || ˋॱ == 1)) {
                            throw fwi.ˋ("PROTOCOL_ERROR SETTINGS_ENABLE_PUSH != 0 or 1", new Object[0]);
                        }
                    case 3:
                        ॱˊ = 4;
                        break;
                    case 4:
                        ॱˊ = 7;
                        if (ˋॱ >= 0) {
                            break;
                        }
                        throw fwi.ˋ("PROTOCOL_ERROR SETTINGS_INITIAL_WINDOW_SIZE > 2^31 - 1", new Object[0]);
                    case 5:
                        if (ˋॱ >= 16384 && ˋॱ <= 16777215) {
                            break;
                        }
                        throw fwi.ˋ("PROTOCOL_ERROR SETTINGS_MAX_FRAME_SIZE: %s", Integer.valueOf(ˋॱ));
                        break;
                    default:
                        break;
                }
                o_fwl.ˊ(ॱˊ, ˋॱ);
            }
            cVar.ॱ(false, o_fwl);
        }
    }

    private void ᐝ(c cVar, int i, byte b, int i2) throws IOException {
        short s = (short) 0;
        if (i2 == 0) {
            throw fwi.ˋ("PROTOCOL_ERROR: TYPE_PUSH_PROMISE streamId == 0", new Object[0]);
        }
        if ((b & 8) != 0) {
            s = (short) (this.ˎ.ʻ() & 255);
        }
        cVar.ॱ(i2, this.ˎ.ˋॱ() & ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED, ॱ(ˋ(i - 4, b, s), s, b, i2));
    }

    private void ʽ(c cVar, int i, byte b, int i2) throws IOException {
        boolean z = true;
        if (i != 8) {
            throw fwi.ˋ("TYPE_PING length != 8: %s", Integer.valueOf(i));
        } else if (i2 != 0) {
            throw fwi.ˋ("TYPE_PING streamId != 0", new Object[0]);
        } else {
            int ˋॱ = this.ˎ.ˋॱ();
            int ˋॱ2 = this.ˎ.ˋॱ();
            if ((b & 1) == 0) {
                z = false;
            }
            cVar.ˊ(z, ˋॱ, ˋॱ2);
        }
    }

    private void ʻ(c cVar, int i, byte b, int i2) throws IOException {
        if (i < 8) {
            throw fwi.ˋ("TYPE_GOAWAY length < 8: %s", Integer.valueOf(i));
        } else if (i2 != 0) {
            throw fwi.ˋ("TYPE_GOAWAY streamId != 0", new Object[0]);
        } else {
            int ˋॱ = this.ˎ.ˋॱ();
            int i3 = i - 8;
            fwf ˏ = fwf.ˏ(this.ˎ.ˋॱ());
            if (ˏ == null) {
                throw fwi.ˋ("TYPE_GOAWAY unexpected error code: %d", Integer.valueOf(r0));
            }
            fxh o_fxh = fxh.ˏ;
            if (i3 > 0) {
                o_fxh = this.ˎ.ˋ((long) i3);
            }
            cVar.ˊ(ˋॱ, ˏ, o_fxh);
        }
    }

    private void ॱॱ(c cVar, int i, byte b, int i2) throws IOException {
        if (i != 4) {
            throw fwi.ˋ("TYPE_WINDOW_UPDATE length !=4: %s", Integer.valueOf(i));
        }
        long ˋॱ = ((long) this.ˎ.ˋॱ()) & 2147483647L;
        if (ˋॱ == 0) {
            throw fwi.ˋ("windowSizeIncrement was 0", Long.valueOf(ˋॱ));
        } else {
            cVar.ॱ(i2, ˋॱ);
        }
    }

    public void close() throws IOException {
        this.ˎ.close();
    }

    static int ˏ(fxg o_fxg) throws IOException {
        return (((o_fxg.ʻ() & 255) << 16) | ((o_fxg.ʻ() & 255) << 8)) | (o_fxg.ʻ() & 255);
    }

    static int ˋ(int i, byte b, short s) throws IOException {
        if ((b & 8) != 0) {
            short s2 = i - 1;
        }
        if (s <= s2) {
            return (short) (s2 - s);
        }
        throw fwi.ˋ("PROTOCOL_ERROR padding %s > remaining length %s", Short.valueOf(s), Integer.valueOf(s2));
    }
}
